package com.bg.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bg.account.entity.Account;
import com.bg.account.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bg
 * @since 2023-02-09
 */
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private IAccountService AccountService;


    @RequestMapping("/reduce")
    public void reduce(String userId, BigDecimal amount) {
        QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Account account = AccountService.getOne(queryWrapper);
        if (null == account)
            throw new RuntimeException("异常:模拟业务异常:账户异常");

        if (account.getAmount().compareTo(amount) == -1)
            throw new RuntimeException("异常:模拟业务异常:资金不足");
        BigDecimal subtract = account.getAmount().subtract(amount);
        account.setAmount(subtract);
        AccountService.updateById(account);

    }
}
